<?php
include 'model/Table.php';

class productModel {	

	function __construct($pagesize = 12) {
		$this -> tbl_product = new Table("tbl_product");
		$this -> pagesize = $pagesize;
		$this -> pagecount = 1;
		$this -> mysqli = DAO::connect();
	}

	function __destruct() {
		if ($this -> mysqli)
			$this -> mysqli -> close();
	}

	function selectProductDetail($ProID) {
		if ($rows = $this -> tbl_product -> select("*", "WHERE ProductID=$ProID")) {
			return $rows;
		}
	}

	function selectProducts($page = 1) {
		if ($rows = $this -> tbl_product -> select("*", "", $this -> pagesize, $page)) {
			$this -> pagecount = $this -> tbl_product -> pageCount("*", "", $this -> pagesize);
			return $rows;
		}
	}

	function selectCategories() {
	   
		if ($rows = $this -> tbl_product -> select("DISTINCT Category")) {
			return $rows;
		}

	}

	function selectProductsByCategory($Category, $page = 1) {

		if ($rows = $this -> tbl_product -> select("*", "WHERE Category='" . $Category . "'", $this -> pagesize, $page)) {
			$this -> pagecount = $this -> tbl_product -> pageCount("WHERE Category='" . $Category . "'", $this -> pagesize);
			return $rows;
		}

	}

	public function selectNewProducts($page = 1) {
		if ($rows = $this -> tbl_product -> select("*", "ORDER BY ProductID DESC", $this -> pagesize, $page)) {
			$this -> pagecount = $this -> tbl_product -> pageCount("ORDER BY ProductID DESC", $this -> pagesize);
			return $rows;
		}
	}

	function getExtension($str) {
		$i = strrpos($str, ".");
		if (!$i) {
			return "";
		}
		$l = strlen($str) - $i;
		$ext = strtolower(substr($str, $i + 1, $l));
		return $ext;
	}

	private function editImage($FILE, $name) {
		$link = "web/images/product/";
		$link_thumb = "web/images/product/thumb/";
		$max_size = 500;
		$new_w = 107;
		$new_h = 180;
		$result = "Up hinh FAIL";

		if ($FILE['name']) {

			$ext = $this -> getExtension(stripslashes($FILE['name']));

			if ($FILE['type'] == "image/jpg") {
				return 'Unknown extension! Please use .jpg files only.';
			}

			if ($FILE['size'] > $max_size * 1024) {
				return 'You have exceeded the 1MB size limit!';
			}

			$image_name = $link . 'product_' . $name;
			if (!copy($FILE['tmp_name'], $image_name)) {
				return 'Copy unsuccessful!';
			}

			$thumb_name = $link_thumb . 'thumb_' . $name;
			$source = imagecreatefromjpeg($image_name);
			$old_x = imagesx($source);
			$old_y = imagesy($source);
			$ratio1 = $old_x / $new_w;
			$ratio2 = $old_y / $new_h;
			if ($ratio1 > $ratio2) {
				$thumb_w = $new_w;
				$thumb_h = $old_y / $ratio1;
			} else {
				$thumb_h = $new_h;
				$thumb_w = $old_x / $ratio2;
			}			
			$tmp_img = imagecreatetruecolor($thumb_w, $thumb_h);
			imagecopyresized($tmp_img, $source, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_x, $old_y);
			imagejpeg($tmp_img, $thumb_name, 100);
			$result = "successful";

		}
		return $result;
	}

	public function insertProduct($category, $quantity, $name, $description, $price, $FILE) {
		$this -> tbl_product -> message = "Fail";
		$imageName = date('YmdHis') . ".jpg";
		$result = $this -> editImage($FILE, $imageName);
		if ($result != "successful") {
			return $result;
		}

		$date = date('Y-m-d H:i:s');
		$data = array("Category" => $category, "Quantity" => $quantity, "Name" => $name, "DateModify" => $date, "Description" => $description, "ImageName" => $imageName, "Price" => $price);
		$this -> tbl_product -> insert($data);
		return $this -> tbl_product -> message;
	}

	public function modifyProduct($ProductID, $category, $quantity, $name, $description, $price, $FILE) {

		$data = NULL;
		$date = date('Y-m-d H:i:s');
		if ($FILE['name']) {
			$imageName = date('YmdHis') . ".jpg";
			$result = $this -> editImage($FILE, $imageName);
			if ($result != "successful") {
				return $result;
			}
			$data = array("Category" => $category, "Quantity" => $quantity, "Name" => $name, "DateModify" => $date, "Description" => $description, "ImageName" => $imageName, "Price" => $price);
		} else {
			$data = array("Category" => $category, "Quantity" => $quantity, "Name" => $name, "DateModify" => $date, "Description" => $description, "Price" => $price);
		}

		$this -> tbl_product ->update($data, "ProductID=2");
		return $this -> tbl_product -> message;
	}
	
	public function deleteProduct($ProductID)
	{
		$this->tbl_product->delete("ProductID=$ProductID");
		return $this->tbl_product->message;
	}

}
?>